package cn.kgc.controller;

import cn.kgc.entity.Provider;
import cn.kgc.entity.User;
import cn.kgc.service.ProviderService;
import cn.kgc.util.Common;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("sys/provider")
public class ProviderController {

    @Autowired
    private ProviderService providerService;

    @RequestMapping(value = "toList",method = RequestMethod.GET)
    public String toList(Provider provider, Integer pageNum, Integer pageSize, Model model) {

        if (pageNum == null) {
            pageNum=1;
        }
        if (pageSize == null) {
            pageSize=5;
        }
        PageInfo<Provider> pageInfo
                = providerService.queryByCodeAndName(provider, pageNum, pageSize);
        model.addAttribute("pageInfo", pageInfo);
        return "provider_list";
    }

    @ResponseBody
    @RequestMapping(value = "/queryById",method = RequestMethod.GET,
            produces = "application/json;charset=utf-8")
    public String queryById(Integer id){
        Provider provider = providerService.queryById(id);
        return JSON.toJSONString(provider);
    }

    @RequestMapping("/queryById2")
    public String queryById2(Model model,Integer id){
        Provider provider = providerService.queryById(id);
        model.addAttribute("provider",provider);
        return "provider_detail";
    }

    @ResponseBody
    @RequestMapping(
            value = "/delete",
            method = RequestMethod.GET)
    public String delete(Integer id){
        Integer count = providerService.delete(id);
        if (count>0){
            return "success";
        }
        return "error";
    }

    @RequestMapping("/toSave")
    public String toSave(){
        return "provider_save";
    }

    @ResponseBody
    @RequestMapping(value = "/check",method = RequestMethod.GET)
    public String queryByCode(String code){
        List<Provider> list = providerService.queryByCode(code);
        if (null!=list && list.size()>0){
            //编号重复
            return "error";
        }
        //没重复
        return "success";
    }

    //查询所有供应商
    @ResponseBody
    @RequestMapping(value = "/queryAll",method = RequestMethod.GET,
    produces = {"application/json;charset=utf-8"})
    public String queryAll(){
        List<Provider> list = providerService.queryAll();
        return JSON.toJSONString(list);
    }

    //添加供应商方法
    @ResponseBody
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public String save(Provider provider, HttpSession session){
        //获取创建者
        User loginUser = (User)session.getAttribute(Common.LOGIN_USER);
        provider.setCreatedBy(loginUser.getId());
        Integer count = providerService.save(provider);
        if (count>0){
            return "success";
        }
        return "error";
    }

    @RequestMapping(value = "/toUpdate",method = RequestMethod.GET)
    public String toUpdate(Integer id,Model model){
        Provider provider = providerService.queryById(id);
        model.addAttribute("provider",provider);
        return "provider_update";
    }

    @ResponseBody
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String update(Provider provider,HttpSession session){
        User loginUser = (User)session.getAttribute(Common.LOGIN_USER);
        provider.setModifyBy(loginUser.getId());
        Integer count = providerService.update(provider);
        if (count>0){
            return "success";
        }
        return "error";
    }
}
